package com.example.ordersystem.service;

import com.example.ordersystem.entity.Dish;
import com.example.ordersystem.repository.DishRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishService {
    @Autowired
    private DishRepository dishRepository;

    public List<Dish> getAllDishes() {
        return dishRepository.findAll();
    }

    public Dish getDishById(Long id) {
        return dishRepository.findById(id).orElse(null);
    }

    public Dish saveDish(Dish dish) {
        return dishRepository.save(dish);
    }

    public List<String> getAllCategories() {
        return dishRepository.findAll().stream()
                .map(Dish::getCategory)
                .filter(c -> c != null && !c.isEmpty())
                .distinct()
                .collect(Collectors.toList());
    }

    public List<Dish> getDishesByCategory(String category) {
        return dishRepository.findAll().stream()
                .filter(d -> category.equals(d.getCategory()))
                .collect(Collectors.toList());
    }

    @Cacheable(value = "hotDishes")
    public List<Dish> getHotDishes() {
        // 这里可以根据实际业务调整为销量/浏览量前几的菜品
        return dishRepository.findAll();
    }
} 